package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.base.Result;
import com.atguigu.spzx.model.vo.base.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author yaya
 * @Create 2024/6/18 18:11
 * Description:
 */
@RestController
@RequestMapping("/admin/system/index")
@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class indexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    /**
     * 后台用户登录
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
   public Result login(@RequestBody LoginDto loginDto){

        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.ok(loginVo);
   }

    /**
     * 使用token获取用户信息
     * @param token
     * @return
     */
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        SysUser sysUser = sysUserService.getUserInfo(token) ;
        return Result.ok(sysUser) ;
    }

    /**
     * 获取验证码64图片码，和redis存储的key
     * @return
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.ok(validateCodeVo) ;
    }

    /**
     * 登出
     * @param token
     * @return
     */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token) ;
        return Result.ok(null ) ;
    }


}
